package com.example.blogsystem.controller;

import com.example.blogsystem.common.AjaxResult;
import com.example.blogsystem.common.GlobalVariable;
import com.example.blogsystem.common.PassWordUtils;
import com.example.blogsystem.common.UserSessionUtils;
import com.example.blogsystem.entity.UserInfo;
import com.example.blogsystem.entity.vo.UserInfoVo;
import com.example.blogsystem.mapper.ArticleMapper;
import com.example.blogsystem.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private ArticleMapper articleMapper;

    @RequestMapping("/register")
    public AjaxResult register(UserInfo userInfo) {
        // 非空校验
        if (userInfo == null || !StringUtils.hasLength(userInfo.getUsername()) ||
                !StringUtils.hasLength(userInfo.getPassword())) {
            return AjaxResult.fail(-1, "非法参数");
        }
        userInfo.setPassword(PassWordUtils.encrypt(userInfo.getPassword()));
        return AjaxResult.success(userService.register(userInfo));
    }

    @RequestMapping("/login")
    public AjaxResult login(HttpServletRequest request, String username, String password) {
        // 非空校验
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return AjaxResult.fail(-1, "非法请求");
        }

        // 查询数据库
        UserInfo userInfo = userService.getUserInfoByName(username);
        if (userInfo != null && userInfo.getId() > 0) {
            // 有效数据
            if (PassWordUtils.check(password, userInfo.getPassword())) {
                // 保存用户的 session
                HttpSession session = request.getSession();
                session.setAttribute(GlobalVariable.USER_SESSION_KEY, userInfo);

                System.out.println("登录成功!");

                // 给前端返回数据前,将密码设置为空
                userInfo.setPassword("");
                return AjaxResult.success(userInfo);
            }
        }
        System.out.println("登录失败!");
        return AjaxResult.success(0, null);
    }

    @RequestMapping("/showuserinfo")
    public AjaxResult getShowUserInfo(HttpServletRequest request) {
        UserInfoVo userInfoVo = new UserInfoVo();
        // 1.获取当前的用户(从session中获取)
        UserInfo userInfo = UserSessionUtils.getSessionUser(request);
        if (userInfo == null) {
            return AjaxResult.fail(-1, "非法请求");
        }
        // Spring提供的深克隆方法
        // userInfoVo.setUsername(userInfo.getUsername());
        BeanUtils.copyProperties(userInfo, userInfoVo);
        // 2.得到用户的文章数量
        int artCount = articleMapper.getArtCount(userInfo.getId());
        userInfoVo.setArtCount(artCount);
        return AjaxResult.success(userInfoVo);
    }

    @RequestMapping("/logout")
    public AjaxResult logout(HttpSession session) {
        session.removeAttribute(GlobalVariable.USER_SESSION_KEY);
        return AjaxResult.success(1);
    }

    @RequestMapping("/getusername")
    public AjaxResult getUsername(Integer id) {
        if (id == null && id <= 0) {
            return AjaxResult.fail(-1, "非法参数");
        }

        String username = userService.getUsername(id);
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(username);
        System.out.println("username = " + username);

        if (username == null) {
            return AjaxResult.fail(-1, "非法参数");
        }
        return AjaxResult.success(userInfo);
    }
}
